package com.example.deliveryapp;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class RestaurantAdapter extends RecyclerView.Adapter<RestaurantAdapter.RestaurantViewHolder> {

    private Context context;
    private List<Restaurant> restaurants;
    private OnItemClickListener onItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    public RestaurantAdapter(Context context, List<Restaurant> restaurants) {
        this.context = context;
        this.restaurants = restaurants;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    @NonNull
    @Override
    public RestaurantViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_restaurant, parent, false);
        return new RestaurantViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RestaurantViewHolder holder, int position) {
        Restaurant restaurant = restaurants.get(position);
        holder.itemView.setOnClickListener(v -> onItemClickListener.onItemClick(position));
        holder.restaurantName.setText(restaurant.getName());
        holder.restaurantDescription.setText(restaurant.getDescription());
        holder.restaurantRating.setText(String.format("%.1f", restaurant.getRating()));
        holder.restaurantCover.setImageResource(restaurant.getCoverImageResourceId());
        holder.restaurantAvatar.setImageResource(restaurant.getAvatarImageResourceId());
    }

    @Override
    public int getItemCount() {
        return restaurants.size();
    }

    static class RestaurantViewHolder extends RecyclerView.ViewHolder {
        ImageView restaurantCover;
        ImageView restaurantAvatar;
        TextView restaurantName;
        TextView restaurantDescription;
        TextView restaurantRating;

        public RestaurantViewHolder(@NonNull View itemView) {
            super(itemView);
            restaurantCover = itemView.findViewById(R.id.restaurant_cover);
            restaurantAvatar = itemView.findViewById(R.id.restaurant_avatar);
            restaurantName = itemView.findViewById(R.id.restaurant_name);
            restaurantDescription = itemView.findViewById(R.id.restaurant_description);
            restaurantRating = itemView.findViewById(R.id.restaurant_rating);
        }
    }
}